﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Supermarket;
using System.Data.SqlClient;
using XLSupermarket.Cashier;
using XLSupermarket.Financial;

namespace XLSupermarket
{
    public partial class LoginForm : Form
    {
        /// <summary>
        /// 构造方法
        /// </summary>
        public LoginForm()
        {
            InitializeComponent();
        }
        public static string VerifyCode;//验证码字符串
        public static string UserName_Value;//当前登录用户名
        bool Count_Name = true;
        bool Count_Pwd = true;
        /// <summary>
        /// 验证码的功能模块
        /// </summary>
        private void GetVerifyCode() {
            //验证码功能
            {
                //创建一个位图
                Bitmap Bmp = new Bitmap(72, 34);//创建一个Bitmap的位图
                //根据新建的Bitmap位图，创建画布
                Graphics Grc = Graphics.FromImage(Bmp);
                //创建随机数
                Random Rdm = new Random(Guid.NewGuid().GetHashCode());
                //定义一个字符串用来存放5位随机数
                string sRnd = "";
                //for循环产生4个随机数，然后构成一个字符串
                for (int i = 0; i < 4; i++)
                {
                    int iNum = Rdm.Next(0, 10);
                    sRnd += iNum.ToString();
                }
                VerifyCode = sRnd;
                char[] strRnd = sRnd.ToCharArray();
                //定义一个数组，存放n种字体
                string[] Fonts = { "微软雅黑", "幼圆", "楷体", "宋体", "隶书", "仿宋" };
                //定义一个数组，存放n种颜色
                Color[] Colors = { Color.Red, Color.Yellow, Color.Yellow, Color.Black, Color.Blue, Color.Green, Color.Pink };
                for (int i = 0; i < 4; i++)
                {
                    //创建坐标
                    Point point = new Point(i * 15, 5);
                    Grc.DrawString(strRnd[i].ToString(), new Font(Fonts[Rdm.Next(0, 6)], 20, FontStyle.Bold), new SolidBrush(Colors[Rdm.Next(0, 7)]), point);
                }
                //画线
                int iBmpW = Bmp.Width;
                int iBmpH = Bmp.Height;
                for (int i = 0; i < 4; i++)
                {
                    //两点一线
                    Point pStare = new Point(Rdm.Next(0, iBmpW), Rdm.Next(0, iBmpH));//开始点
                    Point pEnd = new Point(Rdm.Next(0, iBmpW), Rdm.Next(0, iBmpH));//结束点
                    Grc.DrawLine(new Pen(Colors[Rdm.Next(0, 7)]), pStare, pEnd);//画线
                }
                //噪点
                for (int i = 0; i < 100; i++)
                {
                    Point pRnd = new Point(Rdm.Next(0, iBmpW), Rdm.Next(0, iBmpH));//点坐标
                    Bmp.SetPixel(pRnd.X, pRnd.Y, Colors[Rdm.Next(0, 7)]);//画点
                }
                //把图片给到验证码控件
                PB_Verifycode.Image = Bmp;
            }
        }
        /// <summary>
        /// 账号登录模块
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BT_Login_Click(object sender, EventArgs e)
        {
            string userName = Text_User.Text;//账号
            string pwd = Text_pwd.Text;//密码
            string sql = "select userName,Password from EP_Number where Password='" + pwd + "' and UserName='" + userName + "'";
            string sql_up = "select Staff_Name from EP_information ei,EP_Number en where ei.EP_ID = en.EP_ID and UserName='" + userName + "'";
            DataTable dt = DBHelper.FillData(sql_up);//查看职位
            SqlDataReader sdr = DBHelper.Readers(sql);//验证是否存在该账号
            if (Text_User.Text == " " || Text_pwd.Text == " ")
            {
                MessageBox.Show("账号和密码均不能为空！");
                GetVerifyCode();
            }
            else if (Text_Verifycode.Text.Equals(VerifyCode))
            {
                if (sdr.Read())
                {
                    if (dt.Rows.Count > 0)
                    {
                        string path = dt.Rows[0]["Staff_Name"].ToString();
                        UserName_Value = userName;
                        DBHelper.Close();
                        switch (path)
                        {
                            case "店长":
                                MessageBox.Show("登录成功！\t店长~欢迎您！", "提示", MessageBoxButtons.OK);
                                MainInterface.MainForm MainForm = new MainInterface.MainForm();
                                this.Hide();
                                MainForm.Show();
                                break;
                            case "财务":
                                MessageBox.Show("登录成功！\t财务~欢迎您！", "提示", MessageBoxButtons.OK);
                                FinancialForm ff = new FinancialForm();
                                this.Hide();
                                ff.Show();
                                break;
                            case "管理员":
                                MessageBox.Show("登录成功！\t管理员~欢迎您！", "提示", MessageBoxButtons.OK);
                                Admin.AdminForm AA = new Admin.AdminForm();
                                this.Hide();
                                AA.Show();
                                break;
                            case "仓库":
                                Storehouse.Storehouse Sh = new Storehouse.Storehouse();
                                this.Hide();
                                Sh.Show();
                                MessageBox.Show("登录成功！\t仓库~欢迎您！", "提示", MessageBoxButtons.OK);
                                break;
                            case "收银员":
                                MessageBox.Show("登录成功！\t收银员~欢迎您！", "提示", MessageBoxButtons.OK);
                                Cashier.Cashier cashier = new Cashier.Cashier();
                                this.Hide();
                                cashier.Show();
                                break;
                            default:
                                MessageBox.Show("未查询到该职务！请检查您的账号信息是否录入正确！");
                                GetVerifyCode();
                                break;
                        }
                    }
                    else
                    {
                        MessageBox.Show("未查询到该职务！请检查您的账号信息是否录入正确！");
                        GetVerifyCode();
                    }
                }
                else
                {
                    MessageBox.Show("账号或密码输入错误！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    GetVerifyCode();
                }
            }
            else {
                MessageBox.Show("验证码输入错误！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Text_Verifycode.Text = "";
                GetVerifyCode();
            }
            DBHelper.Close();
        }
        /// <summary>
        /// 验证码4位警告
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Text_Verifycode_TextChanged(object sender, EventArgs e)
        {
            if ((Text_Verifycode.Text).Length > 4)
            {
                MessageBox.Show("验证码只有四位哦！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
        }
        /// <summary>
        /// 点击小眼睛会显示密码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
        {
            Text_pwd.UseSystemPasswordChar = false;
        }
        /// <summary>
        /// 松开小眼睛会隐藏密码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBox2_MouseUp(object sender, MouseEventArgs e)
        {
            Text_pwd.UseSystemPasswordChar = true;
        }
        /// <summary>
        /// 登录头像判断
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Text_User_TextChanged(object sender, EventArgs e)
        {
            if (Count_Pwd)
            {
                Text_pwd.Text = "";
                Count_Pwd = false;
            }
            Text_pwd.UseSystemPasswordChar = true;
            this.Text_pwd.Font = new System.Drawing.Font("华文行楷", 14F);
            //判断是存在该用户名来判断是否有该账号
            string userName = Text_User.Text;//账号
            string sql = "select Src from EP_Number where UserName='" + userName + "'";
            DataTable sdr = DBHelper.FillData(sql);
            if (sdr.Rows.Count > 0)
            {
                string path = sdr.Rows[0]["Src"].ToString();
                FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
                Image img = Image.FromStream(fs);
                PB_Photo.Image = img;
                fs.Close();
            }
            else
            {
                string path = "..\\..\\Image\\Img\\UserDefulte.png";
                FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
                Image img = Image.FromStream(fs);
                PB_Photo.Image = img;
                fs.Close();
            }
        }
        /// <summary>
        /// 点击图片刷新验证码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PB_Verifycode_Click(object sender, EventArgs e)
        {
            GetVerifyCode();
        }
        /// <summary>
        /// 进入应用加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LoginForm_Load(object sender, EventArgs e)
        {
            //密码不可见
            //Text_pwd.UseSystemPasswordChar = true;
            //验证码刷新
            GetVerifyCode();
        }
        /// <summary>
        /// 退出按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BT_Quit_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("您确认退出吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (result == DialogResult.OK) System.Environment.Exit(0);
        }
        /// <summary>
        /// 退出时判断
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LoginForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult result = MessageBox.Show("您确认退出吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (result == DialogResult.Cancel) e.Cancel = true;
        }
        /// <summary>
        /// 第一次单机用户名
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Text_User_Click(object sender, EventArgs e)
        {
            if (Count_Name)
            {
                Text_User.Text = "";
                Count_Name = false;
            }
            this.Text_User.Font = new System.Drawing.Font("宋体", 14F);
        }
        /// <summary>
        /// 点击验证码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Text_Verifycode_Click(object sender, EventArgs e)
        {
            Text_Verifycode.Text = "";
            this.Text_Verifycode.Font = new System.Drawing.Font("宋体", 12F);
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Lable_ForgetThePwd_Click(object sender, EventArgs e)
        {
            Account.ChangePassword.UpPwd ACPUP = new Account.ChangePassword.UpPwd();
            ACPUP.ShowDialog();
        }
    }
}
